{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d9b44944",
   "metadata": {},
   "source": [
    "# Head First python 练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "361392bb",
   "metadata": {},
   "source": [
    "## A-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52c73775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['i', 'i', 'a']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "found=[]\n",
    "word = \"milliways\"\n",
    "for letter in  word:\n",
    "    if letter in vowels:\n",
    "        print(letter)\n",
    "        found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82d1f887",
   "metadata": {},
   "source": [
    "## A-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "060ab92d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']\n"
     ]
    }
   ],
   "source": [
    "phrase=\"Don't panic\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "448a85ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.remove('D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9de8f1f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5cfac609",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    plist.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "129eb895",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', \"'\", 't', ' ', 'p', 'a']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e810e2de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', \"'\", 't', ' ', 'p', 'a']\n"
     ]
    }
   ],
   "source": [
    "plist.insert(2,plist[-3])\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "227db670",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', \"'\", 't', 'a', ' ', 'p', 'a']\n"
     ]
    }
   ],
   "source": [
    "plist.insert(5,plist[-1])\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ee9ed45c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist.pop(3)\n",
    "plist.pop(-3)\n",
    "plist.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2be928ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c5518839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "new_phrase= ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9ff4ab3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "# 课本的方案\n",
    "phrase=\"Don't panic!\"\n",
    "plist=list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ac3f1ddb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', 't', ' ', 'p', 'a']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.remove('D') \n",
    "plist.pop(2) \n",
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "56ecf35d",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.extend([plist.pop(),plist.pop()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a3e8f686",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', 't', ' ', 'a', 'p']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "947cc9c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', ' ', 't', 'a', 'p']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist.insert(2,plist.pop(3))\n",
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2c81747b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'on tap'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_phrase=''.join(plist)\n",
    "new_phrase"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
